from pathlib import Path

from PySide6.QtCore import Signal, Qt
from PySide6.QtWidgets import QVBoxLayout, QFrame, QApplication, QHBoxLayout, QWidget
from qfluentwidgets import (PushButton, TitleLabel, InfoBar, CaptionLabel, TransparentPushButton, FluentIcon, LineEdit,
                            LineEditButton, SubtitleLabel)

from components import LineEditFrame, TableFrame


class WordFrameBase(QFrame):
    returned = Signal()
    started = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()

    def initUI(self):
        self.vBox = QVBoxLayout(self)
        self.vBox.setContentsMargins(10, 10, 10, 10)
        self.vBox.setSpacing(10)
        self.vBox.setAlignment(Qt.AlignTop | Qt.AlignVCenter)

        self.titleLabel = TitleLabel()
        self.titleLabel.setAlignment(Qt.AlignCenter)
        self.openFileFrame = LineEditFrame()
        self.openFileFrame.setLabelText('选择word文件目录：')
        self.openFileFrame.bodyLabel.setFixedWidth(150)
        self.saveFileFrame = LineEditFrame()
        self.saveFileFrame.setLabelText('选择保存pdf文件目录：')
        self.saveFileFrame.bodyLabel.setFixedWidth(150)

        self.startButton = PushButton('开始')

        self.vBox.addWidget(self.titleLabel)
        self.vBox.addWidget(self.openFileFrame)
        self.vBox.addWidget(self.saveFileFrame)
        self.vBox.addWidget(self.startButton)

        self.startButton.clicked.connect(self.started.emit)

#     def set_contentLabel_text(self, _t):
#         self.contentLabel.setText(
#             f'''
# <html><head/><body><p><span style=" font-family:'宋体','monospace'; font-size:16pt; color:#ff0000;">注意：</span>
# <span style=" font-family:'宋体','monospace'; color:#ff8b39;"><br/>
# </span><span style=" font-family:'宋体','monospace'; font-size:12pt; color:#000000;">请将需要转换的</span>
# <span style=" font-family:'JetBrains Mono','monospace'; font-size:12pt; color:#000000;">{_t}</span>
# <span style=" font-family:'宋体','monospace'; font-size:12pt; color:#000000;">文件放到单独的文件夹中，再进行转换。</span>
# </p></body></html>
#             '''
#         )

    def setTitle(self, text):
        self.titleLabel.setText(text)
        self.update()

    def setLabelText(self, t1, t2):
        self.openFileFrame.setLabelText(t1)
        self.saveFileFrame.setLabelText(t2)
        self.update()

    def lineEditText(self):
        openFile, saveFile = self.openFileFrame.lineEditText(), self.saveFileFrame.lineEditText()
        if openFile == '' or saveFile == '':
            InfoBar.warning('提示', '请选择路径信息', parent=self.parent())
            return False, False
        elif not Path(openFile).is_dir() or not Path(saveFile).is_dir():
            InfoBar.warning('提示', '路径信息错误', parent=self.parent())
            return False, False
        else:
            return self.openFileFrame.lineEditText(), self.saveFileFrame.lineEditText()

    def clear(self):
        self.openFileFrame.clear()
        self.saveFileFrame.clear()


class TableFrameBase(QFrame):
    added = Signal()
    started = Signal()
    saved = Signal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.title = TitleLabel()
        self.table = TableFrame()

        self.lineEdit = LineEdit()
        self.openFileButton = LineEditButton(FluentIcon.FOLDER, self.lineEdit)
        self.lineEdit.hBoxLayout.addWidget(self.openFileButton)
        self.lineEdit.setClearButtonEnabled(True)
        self.lineEdit.setPlaceholderText('请选择文件保存路径..')

        self.addButton = TransparentPushButton()
        self.addButton.setIcon(FluentIcon.ADD)
        self.addButton.setText('添加文件')
        self.addButton.setFixedWidth(200)

        self.startButton = PushButton()
        self.startButton.setText('开始')

        self.openFileButton.clicked.connect(self.saved.emit)
        self.addButton.clicked.connect(self.added.emit)
        self.startButton.clicked.connect(self.started.emit)

        self.initLayout()

    def initLayout(self):
        self.vBox = QVBoxLayout(self)
        self.vBox.setContentsMargins(0, 0, 0, 0)
        self.vBox.setSpacing(10)

        self.hBox = QHBoxLayout()
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(10)
        self.hBox.addWidget(self.lineEdit)
        self.hBox.addWidget(self.addButton)

        self.vBox.addWidget(self.title, 0, Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignHCenter)
        self.vBox.addWidget(self.table)
        self.vBox.addLayout(self.hBox)
        self.vBox.addWidget(self.startButton)

    def setTitle(self, title):
        self.title.setText(title)

    def addItem(self, data: list):
        self.table.addItem(data)

    def addItems(self, data: list):
        self.table.addItems(data)

    def setCellWidget(self, row: int, column: int, widget: QWidget):
        self.table.setCellWidget(row, column, widget)

    def getColumnValues(self, col=1):
        column = self.table.getColumnValues(col)
        return column

    def getColumnWidgetText(self, column):
        return self.table.getColumnWidgetText(column=column)

    def setHeaders(self, headers):
        self.table.setHeaders(headers)

    def setLineText(self, text):
        self.lineEdit.setText(text)

    def lineText(self):
        text = self.lineEdit.text()
        if text == '':
            InfoBar.warning('提示', '请选择路径信息', parent=self.parent())
            return False
        # elif not Path(text).is_dir():
        #     InfoBar.warning('警告', '保存路径信息错误', parent=self.parent())
        #     return False
        return text

    def clear(self):
        self.table.clearContents()
        self.lineEdit.clear()

    def rowCount(self):
        return self.table.rowCount()

    def columnCount(self):
        return self.table.columnCount()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = TableFrameBase()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec())
